﻿<cfsilent>
	<cfscript>
	
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		datetimeAdvice = getProperty("serviceFactory").getBean("datetimeAdvice");

		taskProp = getProperty("serviceFactory").getBean("taskPropertyAdvice");
		
		termId = event.getArg("TID");
		courseId = event.getArg("CID");
		taskType = event.getArg("TT");
		taskId = event.getArg("TSK");
		chooserId = event.getArg("SCC");
		
		/* 学籍相关信息 */
		studentId = event.getArg("STU");
		
		/* 回执信息 */
		response = structNew();
		structInsert(response, "success", false, true);
		
		transaction {
			
			switch(taskType){
				case 'P':
					sql = "	SELECT
								tsk.task_prop
							FROM
								t_task tsk
							WHERE
								tsk.tsk_id = :taskId ";
					queryObj = new Query(datasource = application.dnsMaster);
					queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
					rs_taskEntity = queryObj.execute(sql = sql).getResult();
					break;
				case 'C':
					sql = "	SELECT
								tsk.task_prop
							FROM
								t_common_task tsk
							WHERE
								tsk.tsk_id = :taskId ";
					queryObj = new Query(datasource = application.dnsMaster);
					queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
					rs_taskEntity = queryObj.execute(sql = sql).getResult();
					break;
				case 'R':
					sql = "	SELECT
								tsk.task_prop
							FROM
								t_reopen_task tsk
							WHERE
								tsk.tsk_id = :taskId ";
					queryObj = new Query(datasource = application.dnsMaster);
					queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
					rs_taskEntity = queryObj.execute(sql = sql).getResult();
					break;
				case 'T':
					sql = "	SELECT
								tsk.task_prop
							FROM
								t_sport_task tsk
							WHERE
								tsk.tsk_id = :taskId ";
					queryObj = new Query(datasource = application.dnsMaster);
					queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
					rs_taskEntity = queryObj.execute(sql = sql).getResult();
					break;
			}
			
			taskProp.parseProperty( rs_taskEntity.task_prop );
			
			if ( taskProp.getDischarge() ) {
				structInsert(response, "reason", "教学班禁止退课", true);
			}
			else {

				/* 增加选课席位 */
				switch(taskType){
					case 'P':
						sql = "	UPDATE t_task t
								   SET t.task_chooesd = t.task_chooesd -1 
								WHERE
								   t.tsk_id = :taskId ";
						queryObj = new Query(datasource = application.dnsMaster);
						queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
						queryObj.execute(sql = sql);
						break;
					case 'R':
						sql = "	UPDATE t_reopen_task t
								   SET t.task_chooesd = t.task_chooesd -1 
								WHERE
								   t.tsk_id = :taskId ";
						queryObj = new Query(datasource = application.dnsMaster);
						queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
						queryObj.execute(sql = sql);
						break;
					case 'C':
						sql = "	UPDATE t_common_task t
								   SET t.task_chooesd = t.task_chooesd -1 
								WHERE
								   t.tsk_id = :taskId ";
						queryObj = new Query(datasource = application.dnsMaster);
						queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
						queryObj.execute(sql = sql);
						break;
					case 'T':
						sql = "	UPDATE t_sport_task t
								   SET t.task_chooesd = t.task_chooesd -1 
								WHERE
								   t.tsk_id = :taskId ";
						queryObj = new Query(datasource = application.dnsMaster);
						queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
						queryObj.execute(sql = sql);
						break;
				}
				
				/* 删除选课记录 */
				sql = "	DELETE FROM
							t_student_course sc
						WHERE
							sc.scc_id = :chooserId ";
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "chooserId", value = chooserId, cfsqltype = "cf_sql_varchar");
				queryObj.execute(sql = sql);
				
				/* 删除考试安排 */
				sql = "	DELETE FROM
							t_student_exam se
						WHERE
							se.scc_id = :chooserId ";
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "chooserId", value = chooserId, cfsqltype = "cf_sql_varchar");
				queryObj.execute(sql = sql);
				
				/* 删除成绩临时记录 */
				sql = "	DELETE FROM
							t_temp_mark tm
						WHERE
							tm.scc_id = :chooserId ";
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "chooserId", value = chooserId, cfsqltype = "cf_sql_varchar");
				queryObj.execute(sql = sql);
				
				/* 删除对应成绩记录 */
				sql = "	DELETE FROM
							t_student_mark sm
						WHERE
							sm.scc_id = :chooserId ";
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "chooserId", value = chooserId, cfsqltype = "cf_sql_varchar");
				queryObj.execute(sql = sql);
						
				structInsert(response, "success", true, true);
			}
			
		}
		
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>